package client.gui.forms;

/*Copyright 2010 Dietmar Wieser, Margit Mutschlechner and Florian Häser. All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this list of
 conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice, this list
 of conditions and the following disclaimer in the documentation and/or other materials
 provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY Dietmar Wieser, Margit Mutschlechner and Florian Häser ``AS IS''
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 Dietmar Wieser, Margit Mutschlechner and Florian Häser OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 The views and conclusions contained in the software and documentation are those of the
 authors and should not be interpreted as representing official policies, either expressed
 or implied, of Dietmar Wieser, Margit Mutschlechner and Florian Häser.
 */
import java.io.IOException;

import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import client.Controller;
import client.gui.GUIController;
import client.logic.clientmodel.ClientAuthorResponse;
import client.logic.clientmodel.SuggestedAuthor;

/**
 * The PublicationForm displays a list of publications.
 * 
 * @author Dietmar Wieser
 */
public class PublicationsForm extends ListActivity {

	/** true if a bluetooth-scan was done. */
	private boolean isBlueToothRequest;
	/** the selected author */
	private SuggestedAuthor sa = new SuggestedAuthor();
	/** the whole author response */
	private ClientAuthorResponse car = new ClientAuthorResponse();
	/** the list of publications */
	private String[] publications;

	/**
	 * Called when the activity is starting
	 * 
	 * @param savedInstanceState
	 *            If the activity is being re-initialized after previously being
	 *            shut down then this Bundle contains the data it most recently
	 *            supplied in onSaveInstanceState(Bundle).
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = getIntent().getExtras();
		try {
			byte[] tmp = bundle.getByteArray("car");
			if (tmp != null)
				car.resurrect(bundle.getByteArray("car"));
			tmp = bundle.getByteArray("sa");
			if (tmp != null)
				sa.resurrect(bundle.getByteArray("sa"));
		} catch (IOException e) {
			System.out.println("Error while resurrecting ClientAuthorResponse");
		}
		isBlueToothRequest = bundle.getBoolean("isBlueToothRequest");
		publications = bundle.getStringArray("publications");
		initView();
	}

	/**
	 * Initiates the view
	 */
	private void initView() {
		this.setTitle("Publications of " + sa.getAuthor());

		if ((publications != null) && (publications.length > 0)) {
			setListAdapter(new ArrayAdapter<String>(this,
					R.layout.publications, publications));
			ListView lv = getListView();
			lv.setTextFilterEnabled(true);
			lv.setBackgroundColor(Color.WHITE);
			lv.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					GUIController.getInstance().showToast(publications[(int) id]);
				}
			});

		} else {
			GUIController.getInstance().showToast("There exists no publications\nfor the selected author.");
		}
	}
	

	/**
	 * Performs any final cleanup before an activity is destroyed. This can
	 * happen either because the activity is finishing (someone called finish()
	 * on it, or because the system is temporarily destroying this instance of
	 * the activity to save space.
	 */
	public void onDestroy() {
		super.onDestroy();
//		Controller.getInstance().exit();
	}
}
